package FactoryMode.AbstractFacotryMode;

/**
 * 抽象工厂模式
 * 找出变与不变的项，然后对于不变的进行封装（通过接口，继承等方式）
 */

/**
 * 这个实例是展示MySQL和Oracle的连接和命令的发送
 */
public class AbstractFactoryMode {
    public static void main(String[] args) {
        MySqlDataUtils mySqlDataUtils = new MySqlDataUtils();

        IConnection connection = mySqlDataUtils.getConnection();
        connection.getConnected();

        ICommand commanded = mySqlDataUtils.getCommanded();
        commanded.getCommand();
    }
}

//定义连接
interface IConnection {
    void getConnected();
}

//定义命令的发送
interface ICommand {
    void getCommand();
}

//定义数据工具类
interface IDataBaseUtils{
    IConnection getConnection();
    ICommand getCommanded();
}

//细节依赖于抽象
class MySqlConnection implements IConnection {
    @Override
    public void getConnected() {
        System.out.println("Mysql connected.");
    }
}

class MySqlCommand implements ICommand {
    @Override
    public void getCommand() {
        System.out.println("Mysql commanded.");
    }
}

class MySqlDataUtils implements IDataBaseUtils{

    @Override
    public IConnection getConnection() {
        return new MySqlConnection();
    }

    @Override
    public ICommand getCommanded() {
        return new MySqlCommand();
    }
}


